package utilities;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


public class Suggester {
	static String dictionaryfile = "C:/Users/Marco/workspace/Main/dictionary.txt";
	static ArrayList<String> dictionary = new ArrayList<String>();
	static HashMap<String, Integer> newlist = new HashMap<String, Integer>();

	public static void loadDictionary(String filename) throws IOException {
		String line;
		BufferedReader br = new BufferedReader(new FileReader(filename));
		while ((line = br.readLine()) != null) {
			if (line.length() > 2 && line.length() < 26)
				dictionary.add(line);
		}
	}
	
	public String wordSuggester(String word){
        int i;
        String ret = "";
        try {
            loadDictionary(dictionaryfile);
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        for(String s : dictionary){
            i = LevenshteinDistance.computeDistance(s, word);
            if(i<5){
                newlist.put(s, i);
            }
        }

        List<Entry<String, Integer>> entries = new ArrayList<Entry<String, Integer>>(newlist.entrySet());
        Collections.sort(entries, new Comparator<Entry<String, Integer>>() {
            public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) {
                return e1.getValue().compareTo(e2.getValue());
            }
        });

        Map<String, Integer> orderedMap = new LinkedHashMap<String, Integer>();
        for (Entry<String, Integer> entry : entries) {
            orderedMap.put(entry.getKey(), entry.getValue());
        }
     
        for (Entry<String, Integer> e : orderedMap.entrySet()){
        	Integer tmp = e.getValue();
        	if(tmp!=0)
        		ret = e.getKey();
        	break;
        }
        return ret;
    }
}
